package yns.springboot.guide.demo.controller.data.redis;

import java.time.LocalDateTime;

import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import redis.clients.jedis.Jedis;
import redis.clients.jedis.JedisPool;

@RestController
@RequestMapping("/redis/driver")
public class TestRedisDriverController {
	
	// http://localhost:8080/redis/driver
	// http://localhost:8080/redis/driver/add
	
	private final Logger logger = LoggerFactory.getLogger(this.getClass());

	@Autowired
	private Jedis jedis;

	@Autowired
	private JedisPool jedisPool;

	@GetMapping()
	public String testRedisByJedis() {
		logger.info(jedis.get("test"));

		Jedis jedisFromJedisPool = null;
		try {
			jedisFromJedisPool = jedisPool.getResource();
			logger.info(jedisFromJedisPool.get("test"));
		} catch (Exception e) {

		} finally {
			jedisFromJedisPool.close();
		}

		return "TestRedisDriverController -- testRedisByJedis";
	}

	@GetMapping("/add")
	public String addRedisByJedis() {
		
		jedis.set("redis.Jedis", LocalDateTime.now().toString());

		return "TestRedisDriverController -- addRedisByJedis";
	}
}
